package o;

import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import o.fui.a;
import o.fuo.d;
import okhttp3.internal.connection.StreamAllocation;

public final class fwj implements fvv {
    private static final List<String> ˎ = fvg.ˋ("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade");
    private static final List<String> ˏ = fvg.ˋ("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade", ":method", ":path", ":scheme", ":authority");
    private final fut ʽ;
    final StreamAllocation ˊ;
    private final fwk ˋ;
    private final d ॱ;
    private fwh ॱॱ;

    class b extends fxm {
        boolean ˎ = false;
        final /* synthetic */ fwj ˏ;
        long ॱ = 0;

        b(fwj o_fwj, fxw o_fxw) {
            this.ˏ = o_fwj;
            super(o_fxw);
        }

        public long ˊ(fxi o_fxi, long j) throws IOException {
            try {
                long ˊ = ॱ().ˊ(o_fxi, j);
                if (ˊ > 0) {
                    this.ॱ += ˊ;
                }
                return ˊ;
            } catch (IOException e) {
                ॱ(e);
                throw e;
            }
        }

        public void close() throws IOException {
            super.close();
            ॱ(null);
        }

        private void ॱ(IOException iOException) {
            if (!this.ˎ) {
                this.ˎ = true;
                this.ˏ.ˊ.streamFinished(false, this.ˏ, this.ॱ, iOException);
            }
        }
    }

    public fwj(fuv o_fuv, d dVar, StreamAllocation streamAllocation, fwk o_fwk) {
        fut o_fut;
        this.ॱ = dVar;
        this.ˊ = streamAllocation;
        this.ˋ = o_fwk;
        if (o_fuv.ʽॱ().contains(fut.H2_PRIOR_KNOWLEDGE)) {
            o_fut = fut.H2_PRIOR_KNOWLEDGE;
        } else {
            o_fut = fut.HTTP_2;
        }
        this.ʽ = o_fut;
    }

    public fxx ॱ(fus o_fus, long j) {
        return this.ॱॱ.ʻ();
    }

    public void ˋ(fus o_fus) throws IOException {
        if (this.ॱॱ == null) {
            try {
                this.ॱॱ = this.ˋ.ˎ(ˎ(o_fus), o_fus.ˎ() != null);
                this.ॱॱ.ˎ().ˊ((long) this.ॱ.ˋ(), TimeUnit.MILLISECONDS);
                this.ॱॱ.ᐝ().ˊ((long) this.ॱ.ˎ(), TimeUnit.MILLISECONDS);
            } catch (IOException e) {
                this.ˊ.noNewStreams();
                throw e;
            }
        }
    }

    public void ˊ() throws IOException {
        this.ˋ.ˋ();
    }

    public void ॱ() throws IOException {
        this.ॱॱ.ʻ().close();
    }

    public fvb.d ˏ(boolean z) throws IOException {
        fvb.d ˏ = ˏ(this.ॱॱ.ˋ(), this.ʽ);
        if (z && fux.ˏ.ˎ(ˏ) == 100) {
            return null;
        }
        return ˏ;
    }

    public static List<fwb> ˎ(fus o_fus) {
        fui ॱ = o_fus.ॱ();
        List<fwb> arrayList = new ArrayList(ॱ.ˋ() + 4);
        arrayList.add(new fwb(fwb.ˊ, o_fus.ˋ()));
        arrayList.add(new fwb(fwb.ˎ, fwa.ॱ(o_fus.ˏ())));
        String ˋ = o_fus.ˋ("Host");
        if (ˋ != null) {
            arrayList.add(new fwb(fwb.ʼ, ˋ));
        }
        arrayList.add(new fwb(fwb.ˋ, o_fus.ˏ().ˋ()));
        int ˋ2 = ॱ.ˋ();
        for (int i = 0; i < ˋ2; i++) {
            fxh ˏ = fxh.ˏ(ॱ.ॱ(i).toLowerCase(Locale.US));
            if (!ˏ.contains(ˏ.ˊ())) {
                arrayList.add(new fwb(ˏ, ॱ.ˋ(i)));
            }
        }
        return arrayList;
    }

    public static fvb.d ˏ(fui o_fui, fut o_fut) throws IOException {
        a aVar = new a();
        int ˋ = o_fui.ˋ();
        fvy o_fvy = null;
        for (int i = 0; i < ˋ; i++) {
            String ॱ = o_fui.ॱ(i);
            String ˋ2 = o_fui.ˋ(i);
            if (ॱ.equals(":status")) {
                o_fvy = fvy.ॱ("HTTP/1.1 " + ˋ2);
            } else if (!ˎ.contains(ॱ)) {
                fux.ˏ.ˋ(aVar, ॱ, ˋ2);
            }
        }
        if (o_fvy != null) {
            return new fvb.d().ॱ(o_fut).ॱ(o_fvy.ˎ).ˊ(o_fvy.ˊ).ॱ(aVar.ˊ());
        }
        throw new ProtocolException("Expected ':status' header not present");
    }

    public fuy ˎ(fvb o_fvb) throws IOException {
        this.ˊ.eventListener.responseBodyStart(this.ˊ.call);
        return new fvx(o_fvb.ˋ(HttpContants.KEY_CONTENT_TYPE), fvr.ˏ(o_fvb), fxr.ˎ(new b(this, this.ॱॱ.ʼ())));
    }

    public void ˋ() {
        if (this.ॱॱ != null) {
            this.ॱॱ.ॱ(fwf.CANCEL);
        }
    }
}
